(function($){
	var curMutilUserTarget;
	var curMutilUserFill;
	var calendarCfg={
		month:['正月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '腊月'],
		day: ['初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十', 
                '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '廿十',
                '廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '卅十'],
        oldDay:30,
        year:2012-1900,
        newDay:[31,28,31,30,31,30,31,31,30,31,30,31],
        newMonth:[1,2,3,4,5,6,7,8,9,10,11,12],
        curYear:(new Date()).getFullYear()
	}
	var tabPreName="defTab_";
	var activeClass='active';
	var tabUqKey='tab_uq_key';
	var tabHandle=function(){
		//标签数量限制
		this.maxTab=8;
		this.opt=null;
		this.tab=null;
		this.tabIndex="tab_id_";
		this.i=1;
		this.openTabs={};
		this.tabTemplate="<div class='tabTitle canDelete'><span class='text'></span><a class='ctrl' href='javascript:void(0)'>&nbsp;</a></div>";
		this.contentTemplate="<div class='tabItem' align='center'><iframe class='index_frame' frameborder=0 width='100%'></iframe></div>";
		this.pos=null;
		this.contextTemplate="<div id='_tab_context_menu_div'>" +
						    "<li action='close'>关闭</li>" +	
						    "<li action='reflash'>刷新</li>" +	
						    "<li action='closeAll'>关闭所有标签</li>" +
						    "<li action='closeOther'>关闭其他标签</li>" +
						    "<li action='closeRight'>关闭右侧标签</li>" +
						 "</div>";
		this.contextMenu=null;
		this.curContextSrc=null;
	}
	function getTabKey(elem){
		return elem.attr(tabUqKey);
	}
	function getCurTab(elem){
		return elem.parents("div.jquery_tab").find("."+activeClass);
	}
	function getTabInst(elem){
		return elem.parents("div.jquery_tab").data("jqueryTab");
	}
	tabHandle.prototype={
		init:function(param){
			this.opt=param;
			var child=this.tab.children("div.tabTitle");
			var inst=this;
			//设置iframe 高度与宽带  避免外部出现滚动条
			this.pos={
				w:$(document).width()-10,
				h:$(document).height()-this.tab.offset().top-this.tab.height()-10
			};
			child.each(function(i){
				var content=$(inst.opt.content.children("div.tabItem").get(i));
				content.children("iframe").css({
					//width:inst.pos.w+"px",
					height:inst.pos.h+"px"
				});
				inst.save({
					tab:$(this),
					content:content,
					key:tabPreName+i
				});
			});
			child.bind("click",this.showTab);
			$(window).bind("resize",this.resize);
		},
		resize:function(){
			var inst=top.$("#_crm_tab_ctrl_div_").data("jqueryTab");
			var content=top.$("#_crm_tab_content_div_");
			//resize 
			inst.pos.w=document.documentElement.clientWidth-10;
			inst.pos.h=document.documentElement.clientHeight-inst.tab.offset().top-inst.tab.height()-10;
			content.find("iframe").css({
				//width:inst.pos.w+"px",
				height:inst.pos.h+"px"
			});
		},
		showTab:function(e){
			$.initEvent(e);
			var src=$(e.src);
			var inst=getTabInst(src);
			var curKey=getTabKey(src);
			if(!curKey){
				curKey=getTabKey(src.parents(".tabTitle"));
			}
			var oldTab=getCurTab(src);
			var oldKey=getTabKey(oldTab);
			//无变化 不处理
			if(oldKey==curKey){
				return;
			}
			var oldCfg=inst.findTabOpt(oldKey);
			if(oldCfg){
				oldCfg.tab.removeClass(activeClass);
				oldCfg.content.hide();
			}
			var newCfg=inst.findTabOpt(curKey);
			newCfg.tab.addClass(activeClass);
			newCfg.content.show();
		},
		bindCtrl:function(elem){
			var inst=this;
			elem.bind("click",function(e){
				$.stopEvent(e);
				$.initEvent(e);
				var tab=$(e.src).parent();
				if(inst.isActive(tab)){
					inst.closeCurTab(true);
				}
				else{
					inst.closeTabByKey(tab.attr(tabUqKey));
				}
			});
		},
		createMenu:function(){
			var inst=this;
			var div=$(this.contextTemplate).appendTo(document.body);
			div.bind("click",function(e){
				$.stopEvent(e);
				$.initEvent(e);
				inst.doContextMenu($(e.src).attr("action"));
			});
			div.bind("mousedown",function(e){
				e.stopPropagation();
			});
			inst.contextMenu=div;
		},
		isActive:function(elem){
			return elem.hasClass(activeClass);
		},
		doContextMenu:function(action){
			var cfg=this.openTabs[this.curContextSrc.attr(tabUqKey)];
			var frame=cfg.content.children("iframe");
			var src=frame.attr("src");
			var curTab=getCurTab(this.curContextSrc);
			var tabs=$("#_crm_tab_ctrl_div_").children("div.canDelete");
			switch(action){
				case 'close':
							 if(this.isActive(curTab)){
								this.closeCurTab(true);
							 }
							 else{
								 this.closeTabByKey(cfg.key);
							 }
							 break;
				case 'reflash':frame.attr("src",src);this.curContextSrc.trigger("click");break;
				case 'closeAll':for(var i=0;i<tabs.length;i++){
									this.closeTabByKey($(tabs.get(i)).attr(tabUqKey));
								}							
								this.createTab({key:'defTab_0'}, true); 
								break;
				case 'closeOther':
					for(var i=0;i<tabs.length;i++){
						var key=$(tabs.get(i)).attr(tabUqKey);
						if(key!=cfg.key)
							this.closeTabByKey(key);						
					}	
					this.curContextSrc.trigger("click");
					break;
				case 'closeRight':
					var next=this.curContextSrc.nextAll("div.canDelete");
					for(var i=0;i<next.length;i++){
						this.closeTabByKey($(next.get(i)).attr(tabUqKey));
					}
					this.curContextSrc.trigger("click");
					break;
			}
			this.contextMenu.hide();
		},
		showContext:function(e){
			var src=$(e.src);
			if(!src.hasClass("tabTitle")){
				src=src.parent();
			}
			this.curContextSrc=src;
			this.contextMenu.css({
				left:e.pageX+"px",
				top:e.pageY+"px"
			});
			this.contextMenu.show();
		},
		addRight:function(tab){
			var inst=this;
			tab.bind("contextmenu",function(e){
				$.stopEvent(e);
				$.initEvent(e);
				if(!inst.contextMenu){
					inst.createMenu();
				}
				inst.showContext(e);
			})
		},
		getTabTotal:function(){
			return $("#_crm_tab_ctrl_div_").children("div").length;
		},
		createTab:function(cfg,isShow){
			if(this.getTabTotal()>=this.maxTab){
				$.messager.alert("&nbsp;","打开的标签页超过"+this.maxTab+"个,请先关闭其他标签");
				return;
			}
			var temp=this.findTabOpt(cfg.key);
			if(!temp){
				var tab=$(this.tabTemplate).appendTo(this.tab);
				tab.children("span.text").text(cfg.title);
				var ctrl=tab.children("a.ctrl");
				this.bindCtrl(ctrl);
				this.addRight(tab);
				tab.attr("title",cfg.title);
				tab.bind("click",this.showTab);
				tab.bind("mouseover",function(){
					ctrl.css("visibility","visible");
				})
				tab.bind("mouseout",function(){
					ctrl.css("visibility","hidden");
				})
				var content=$(this.contentTemplate).appendTo(this.opt.content);
				content.find("iframe").attr("src",cfg.link);
				content.children("iframe").css({
					//width:this.pos.w+"px",
					height:this.pos.h+"px"
				});
				this.save({
					tab:tab,
					content:content,
					key:cfg.key,
					openFrom:this.tab.find("."+activeClass),
					change:cfg.change
				});
				if(isShow)
					tab.trigger("click");
			}
			else{
				temp.tab.trigger("click");
			}
		},
		findTabOpt:function(key){
			return this.openTabs[key];
		},
		save:function(param){
			this.openTabs[param.key]=param;
			param.tab.attr(tabUqKey,param.key);
		},
		getCurTab:function(){
			var curTab=this.tab.find("."+activeClass);
			return this.openTabs[curTab.attr(tabUqKey)]
		},
		destroyIframe:function(temp){
			var iframe=temp.content.find("iframe");
			temp.tab.remove();
			iframe.get(0).contentWindow.document.write('');
			iframe.get(0).contentWindow.close();
			//iframe.attr("src","");
			//iframe.get(0).contentWindow.location.replace("about:blank");
			iframe.remove();
			temp.content.remove();
			iframe=null;
			temp=null;
		},
		closeCurTab:function(flag){
			var temp=this.getCurTab();
			if(temp){
				this.destroyIframe(temp);
			}
			if(flag&&temp){
				this.getOpenFrom(temp).trigger("click")
			}
			else{
				this.getOpenFrom({}).trigger("click")
			}
			delete this.openTabs[temp.key];
		},
		closeTabAndChange:function(param){
			var curTab=this.getCurTab();
			var openFrom=this.getOpenFrom(curTab);
			var cfg=this.findTabOpt(openFrom.attr(tabUqKey));
			//传递数据、配置
			//有配置change
			var callBack=curTab.change?curTab.change:param.change;
			cfg.content.children("iframe").get(0).contentWindow
			.mydlg.tabFuncCfg[callBack].call(param.data,curTab);
			//curTab.content.
			this.closeCurTab(true);
		},
		getOpenFrom:function(curTab){
			var openFrom=curTab.openFrom;
			if(!openFrom||!openFrom.length||!openFrom.parent().length){
				openFrom=this.tab.children("div.indexTab");
			}
			return openFrom;
		},
		closeTabByKey:function(key){
			var cfg=this.findTabOpt(key);
			if(!cfg)
				return;
			this.destroyIframe(cfg);
			delete this.openTabs[key];
		},
		changeTabSrc:function(param,str){
			var temp=this.getCurTab();
			var title=temp.tab.text();
			if(str){
				title+=str;
			}
			else{
				title=title.replace("(编辑)","");
			}
			param.title=title;
			temp.tab.children("span.text").text(title);
			temp.tab.attr("title",title);
			temp.tab.attr(tabUqKey,param.key);
			temp.content.children("iframe").attr("src",param.link);
			delete this.openTabs[param.oldKey];
			this.save({
				tab:temp.tab,
				content:temp.content,
				key:param.key,
				openFrom:temp.openFrom,
				change:temp.change
			});
			delete param;
		}
	};
	var saveFuncCfg={
		saveGroup:function(inst,name){
			var pe=inst.parent();
			var items=pe.find("div.item");
			var arr=[];
			items.each(function(){
				var temp=$(this);
				var input=temp.find("#"+name);
				var type=temp.find("#"+name+"_type");
				var obj={};
				if(input.val()!=""||type.val()!=""){
					if(input.attr('valueKey')){
						obj[input.attr('valueKey')]=input.val();
						
					}
					else{
						obj.text=input.val();
					}
					obj.type=type.val();
					arr.push(obj);
				}
			});
			inst.val(JSON.stringify(arr));
		},
		saveMoney:function(inst,name){
			var temp=inst.parent();
			var input=temp.find("#"+name);
			var type=temp.find("#"+name+"_type");
			if(input.val()!=""||type.val()!=""){
				var obj={
					amount:input.val(),
					currency:type.val()
				}
				inst.val(JSON.stringify(obj));
			}
		},
		saveCalendar:function(inst,name){
			var temp=inst.parent().parent();
			var obj={
				calendar:temp.find("input.check").val()
			};
			var year=temp.find("select#year");
			var month=temp.find("select#month");
			var day=temp.find("select#day");
			if(year.val()!=""){
				obj.year=year.val();
			}
			if(month.val()!=""){
				obj.month=month.val();
			}
			if(day.val()!=""){
				obj.day=day.val();
			}
			inst.val(JSON.stringify(obj));
		},
		saveContacts:function(inst,name){
			inst.val(JSON.stringify(inst.data("jsonValue")));
		},
		saveArea:function(inst,name){
			addressHandle.saveArea(inst,name);
		}
	}
	function stringify(funcName,name,elem){
		var func=saveFuncCfg[funcName];
		if(!func)
			return;
		(func)(elem,name);
	}
	function bindTarget(elem,rela){
		elem.bind("click",function(e){
			e.stopPropagation();
			$(document).trigger("click");
			rela.data("curTarget",elem);
			rela.css({
				top:elem.offset().top+elem.height()+"px",
				left:elem.offset().left+"px"
			});
			rela.show();
		});
	}
	function bindSelect(select){
		select.children("div").bind("click",function(e){
			var target=select.data("curTarget");
			target.val($(this).text());
			target.css("color","");
			select.hide();
		})
	}
	function bindEvent(tag,select){
		var target=tag.children("input");
		bindTarget(target,select);
	}
	function bindValid(elem){
		var inputs=elem.find("input[validtype=='']");
		inputs.validatebox({
			validtype:elem.attr("validtype")
		});
	}
	function fillYear(elem){
		var i=calendarCfg.curYear-calendarCfg.year;
		for(;i<=calendarCfg.curYear;i++){
			var op=$("<option>").appendTo(elem);
			op.val(i);
			op.text(i);
		}
		//elem.val(calendarCfg.curYear-20);
	}
	function fillMonth(elem,val){
		var oldValue=elem.val();
		elem.html("");
		var month=calendarCfg.newMonth;
		if(val==2){
			month=calendarCfg.month;
			fillOldDay(elem.next());
		}
		else{
			fillDay(elem.next(),oldValue);
		}
		for(var i=1;i<13;i++){
			var op=$("<option>").appendTo(elem);
			op.val(i);
			op.text(month[i-1]);
		}
		elem.val(oldValue);
	}
	function fillOldDay(elem){
		var oldValue=elem.val();
		elem.html("");
		for(var i=1;i<31;i++){
			var op=$("<option>").appendTo(elem);
			op.val(i);
			op.text(calendarCfg.day[i-1]);
		}
		elem.val(oldValue);
	}
	function fillDay(elem,month){
		if(!month||month=="")
			month=1;
		var oldValue=elem.val();
		elem.html("");
		var year=elem.parent().find("select#year").val();	
		var day=calendarCfg.newDay[month-1];
		if(month==2){
			if(year!=""&&year%4==0){
				day+=1;
			}
		}
		for(var i=1;i<=day;i++){
			var op=$("<option>").appendTo(elem);
			op.val(i);
			op.text(i);
		}
		elem.val(oldValue);
	}
	function fillRelaBox(target){
		var total=target.attr("_rela_total");
		var val="";
		for(var i=0;i<total;i++){
			if(target.attr("_rela_"+i))
				val+=target.attr("_rela_"+i);
		}
		target.val(val);
	}
	function pickerMutilUser(){
		var ids=[];
		var textArr=[];
		var jsonValue=[];
		var data=$roleMember.find(">div");
		$(data).each(function(){
			var tempId=parseInt(this.id.replace("tree_",""));
			var tempText=$(this).text();
			
			ids.push(tempId);
			textArr.push(tempText);
			jsonValue.push({
				id:tempId,
				name:tempText
			});
		});
		curMutilUserTarget.text(textArr.join(","));
		curMutilUserFill.val(ids.join(","));
		curMutilUserTarget.data("jsonValue",jsonValue);
		commonWindow.window({
			closed:true
		});
	}
	function showMutilPicker(){
		var content=commonWindow.find("#userTree");
		var select=commonWindow.find(".roleMember");
		select.html("");
		content.html("");
		commonWindow.show();		
		content.listTree({
			dataUrl:'/user/tree',	
			qryUrl:'/user/Search',	
			fillItem:fillItem,
			hasQryTool:true,
			parentDrag:true,
			onDrop:moveDept
		});
		commonWindow.window({
			closed:false,
			modal:true,
			collapsible: false,
			minimizable: false,
			maximizable: false,
			title:'&nbsp',			
			resizable:false,			
			shadow:false
		});
	}
	$.relaValid={
		validUQId:1,
		validUQKey:'relaValidUQID_',
		addValid:function(objA,objB){
			var cfg=objB.data("relaValidCfg");
			var key=objA.data("validKey");
			if(!key){
				key=$.relaValid.validUQKey+$.relaValid.validUQId++;
				objA.data("validKey",key);
			}
			if(!cfg){
				cfg={};
			}
			if(!cfg[key]){
				cfg[key]=objA;
				objB.data("relaValidCfg",cfg);
			}
		}	
	}
	$.bindFuncCfg={
		bindAreaTable:function(val,data,key){
			if(!val){
				return;
			}
			//测试  服务端未解析返回
			//val=JSON.parse(val);
			addressHandle.bindArea($("#"+key),val);
		},
		bindSlider:function(val,data,key){
			$("input[name="+key+"]").each(function(){
				var $this=$(this);
				$this.val(val);
				$this.trigger("blur");
			});
		},
		bindLevel:function(val,data,key){
			$("input[name="+key+"]").each(function(){
				var $this=$(this);
				if(this.value==val){					
					$this.attr("checked",true);
					$this.parent().addClass("radioBox_bg");
					$this.parent().find("span.checkShow").addClass("check");
				}
			});
		},
		bindMoney:function(val,data,key){
			if(!val)
				return;
			$("input[name="+key+"]").each(function(){
				var temp=$(this).parent();
				temp.find("#"+key).val(val.amount);
				temp.find("#"+key+"_type").val(val.currency);
			});
		},
		bindCalendar:function(val,data,key){
			//测试
			//val=JSON.parse(val);
			if(!val)
				return;
			$("input[name="+key+"]").each(function(){
				var temp=$(this).parent().parent();
				temp.find("input#"+key+val.calendar).trigger("click");
				temp.find("select#year").val(val.year);
				temp.find("select#month").val(val.month);
				temp.find("select#day").val(val.day);
			});
		},
		bindAddress:function(val,data,key){
			if(!val)
				return;
			var time=val.time;
			var location=val.location;
			var elem=$("#"+key);
			var curTime=$("<span>").appendTo(elem);
			curTime.text(time);
			var link=$("<a class='mapLink' />").appendTo(elem);
			link.attr("href","javascript:openMap('/static/html/map.html?lon="+location.lon+"&lat="+location.lat+"')");
		},
		bindDetailCust:function(val,data,key){
			if(!val)
				return;
			var elem=$("#"+key).html("<a href='javascript:void(0)'>"+val.name+"</a>");
			mydlg.addEvent({
				elem:elem,
				link:'/customer/detail?id='+val._id,
				title:val.name,
				key:val._id+"_detail"
			});
			
		},
		bindDetailContact:function(val,data,key){
			if(!val)
				return;
			var elem=$("#"+key).html("<a href='javascript:void(0)'>"+val.name+"</a>");
			mydlg.addEvent({
				elem:elem,
				link:'/contact/detail?id='+val._id,
				title:val.name,
				key:val._id+"_detail"
			});
		},
		bindMutilUser:function(val,data,key){
			if(!val)
				return;
			var target=$("input[name="+key+"]");
			var ctrl=$("#"+key);
			var ids=[];
			var names=[];
			var jsonValue=[];
			$(val).each(function(){
				ids.push(this._id);
				names.push(this.userName);
				jsonValue.push({
					id:this._id,
					name:this.userName
				})
			});
			target.val(ids.join(","));
			ctrl.text(names.join(","));
			ctrl.data("jsonValue",jsonValue);
		},
		bindCate:function(val,data,key){
			if(!val)
				return;
			var target=$("input[name="+key+"]");
			target.val(val._id);
			target.parents('#categoryPicker').children('.targetSpan').children(".combo-text").val(val.name);
		}
	}
	$(document).bind("click",function(e){
		e.stopPropagation();
		$("div.select").hide();	
	})
	$.mutilUserPicker=function(){
		curMutilUserTarget=$(this);
		curMutilUserFill=curMutilUserTarget.parent().find("input[name="+curMutilUserTarget.attr("id")+"]");
		commonWindow.find("button.save").removeAttr("onclick").unbind("click").bind("click",pickerMutilUser);
		showMutilPicker();
		var oldVal=curMutilUserTarget.data("jsonValue");
		$(oldVal).each(function(){
			fillMember(this);
		});
	}
	$.fn.levelPicker=function(){
		var $this=$(this);
		$this.find("input[type=radio]").each(function(){
			var input=$(this);
			input.bind("click",function(e){
				oldCheck=input.parent().parent().find("span.check");
				if(oldCheck.length){
					oldCheck.removeClass("check");
					//oldCheck.parent().removeClass("radioBox_bg");
				}
				input.parent().find("span.checkShow").addClass("check");
				//input.parent().addClass("radioBox_bg");
			});
		});
	}
	$.fn.relaBox=function(){
		if(typeof(operation)!="undefined"&&operation=="edit"){
			return;
		}
		var target=this;
		var cfg=this.attr("relas").split(",");
		target.attr("_rela_total",cfg.length);
		var initVal="";
		$(cfg).each(function(i){
			var temp=this.split(":");
			var elem=$(temp[0]);
			if(temp[1]=="change"){
				elem.bind(temp[1],function(){
					target.attr("_rela_"+i,$(this).next().text());
					fillRelaBox(target);
				})
				$(elem.get(0)).trigger("change");
			}
			if(temp[1]=="newCombopicker"){
				elem[temp[1]]({
					onTextChange:function(text){
						target.attr("_rela_"+i,text);
						fillRelaBox(target);
					}
				})	
			}
		});
	}
	$.fn.myCalendar=function(){
		var inst=this;
		var dateKey=this.attr("datekey");
		var ctrl=this.find("input._radio_");
		var year=this.find("select#year");
		var month=this.find("select#month");
		var day=this.find("select#day");
		fillYear(year);
		month.bind("change",function(){
			var type=inst.find("input.check").val();
			if(type==1){
				fillDay(day,this.value);
			}
		})
		year.bind("change",function(){
			var type=inst.find("input.check").val();
			if(type==1){
				fillDay(day,month.val());
			}
		})
		inst.data("myCalendar",{
			dateKey:dateKey
		});
		ctrl.bind("click",function(e){
			$.initEvent(e);
			var elem=$(e.src);
			var oldCheck=elem.parent().find(".check");
			if(oldCheck.val()!=elem.val()){
				oldCheck.attr("checked",false);
				oldCheck.removeClass("check");
				elem.addClass("check");
				elem.attr("checked",true);
				fillMonth(inst.find("select#month"),elem.val());
			}
		});
		$(ctrl.get(0)).trigger("click");
	}
	$.fn.phoneGroup=function(){
		var $this=$(this);
		var template=$this.children(".input_select_template");
		var tag=template.children(".input_select_tag");
		var ctrl=$this.children(".add_module_class");
		var select=$this.children(".select");
		var temp=template.html();
		bindSelect(select);
		bindEvent(tag,select);
		bindValid(template);
		ctrl.bind("click",function(){
			var next=ctrl.next();
			var delRow=next.find(".delItem");
			if(!delRow.length){
				delRow=$("<div class='delItem'>").appendTo(next);		
			}
			delRow.bind("click",function(){
				next.remove();
//				var rows=$this.find(".item");
//				if(rows.length==1){
//					rows.removeClass("item").addClass("input_select_template");
//				}
			});
			$this.children(".item").addClass("canDelete");
			var item=$("<div class='item' />").insertAfter(ctrl);
			var html=$(temp).appendTo(item);
			bindEvent(item.children(".input_select_tag"),select);		
			bindValid(item);
		});
	}	
	$.fn.phoneGroup.bindGroup=function(val,data,key){
		if(!val||val.length==0)
			return;
		//val=JSON.parse(val);
		var input=$('#'+key);
		var top=input.parents("#contentDiv");
		var ctrl=top.find(".add_module_class");		
		var len=val.length;
		var type=$('#'+key+"_type");
		for(var i=0;i<len;i++){
			if(i!=0){
				ctrl.trigger("click");
			}
			var next=ctrl.next();
			type=next.find('#'+key+"_type");
			input=next.find('#'+key);
			if(input.attr('valueKey')){
				input.val(val[i][input.attr('valueKey')]);
			}
			else{
				input.val(val[i].text);
			}
			type.val(val[i].type);
		}
	}
	$.preSave=function(elem){
		var inputs=elem.find("input[type=hidden]");
		inputs.each(function(){
			var name=this.name
			var input=$(this);
			var func=input.attr("preSave");
			if(!name||!func||name=="")
				return;
			stringify(func,name,input);
		});
	}
	$.fn.jqueryTab=function(opt){
		var $this=$(this);
		var inst=$this.data("jqueryTab");
		if(!inst){
			inst=new tabHandle();
			inst.tab=$this;
			inst.init(opt);
			$this.data("jqueryTab",inst);
		}
	}
})(jQuery);
var jquerySlider={
		flag:false,
		initToolInfo:function(e){
			$.initEvent(e);
			var elem=$(e.src);
			var pe=elem.parent();
			if(!pe.hasClass("slider")){
				pe=pe.parent();
			}
			jquerySlider.srcElem=pe.find(".ctrl");
			jquerySlider.line=pe.find(".line");
			jquerySlider.text=pe.find(".text");
			jquerySlider.target=jquerySlider.text.find("input");		
			jquerySlider.curWidth=jquerySlider.line.width();	
			var total=pe.width()-jquerySlider.text.width();
			jquerySlider.maxLen=parseInt(total-jquerySlider.srcElem.width()-5);
		},
		startMove:function(e){
			$.stopEvent(e);
			jquerySlider.initToolInfo(e);	
			jquerySlider.flag=true;
		},
		movejquerySlider:function(e){		
			if(!jquerySlider.flag){
				return;	
			}
			$.stopEvent(e);
			if(!jquerySlider.lastPos){
				jquerySlider.lastPos=$.getPostion(e).x;	
				return;
			}				
			var curx=$.getPostion(e).x;
			var moveLen=curx-jquerySlider.lastPos;
			var lineLen=parseInt(jquerySlider.curWidth+moveLen);
			if(lineLen==0){
					jquerySlider.line.get(0).style.width=lineLen+"px";
					jquerySlider.target.val(0);						
			}
			if(lineLen>0&&lineLen<jquerySlider.maxLen){
					jquerySlider.line.get(0).style.width=lineLen+"px";
					if(jquerySlider.target.val()>1){
						var val=lineLen/(jquerySlider.maxLen-10)*100;
					}
					else{
						var val=lineLen/(jquerySlider.maxLen+5)*100;
					}
					if(val>100){
						val=100;	
					}
					else{
						val=parseInt(val);	
					}					
					jquerySlider.target.val(val);
			}
		},
		endMove:function(e){
			if(e){
				jquerySlider.destroy();
			}
		},
		init:function(elem){
			var $this=$(elem);
			var btn=$this.find(".ctrl");
			$this.find(".text input").bind("blur",function(e){
				$.initEvent(e);
				var val=e.src.value;
				jquerySlider.initToolInfo(e);
				if(val<0||val>100||val.replace(/[0-9]/g,"")!=""){
					e.srcElement.value="0";
					val=0;
				}
				else{
					e.srcElement.value=parseInt(val*100)/100;	
				}
				jquerySlider.line.get(0).style.width=parseInt(val*jquerySlider.maxLen/100)+"px";
				jquerySlider.destroy();	
			});
			btn.bind("mousedown",jquerySlider.startMove);	
			$(document).bind("mousemove",jquerySlider.movejquerySlider);
			$(document).bind("mouseup",jquerySlider.endMove);	
		},
		destroy:function(){
			jquerySlider.flag=false;
			jquerySlider.srcElem=null;
			jquerySlider.line=null;
			jquerySlider.text=null;
			jquerySlider.target=null;
			jquerySlider.maxLen=null;
			jquerySlider.lastPos=null;
		}	
	}

var jqTab={
	curContent:null,
	curTab:null,
	init:function(opt){
		opt.elem.each(function(){
			var $this=$(this);
			$this.find(".subDiv").each(function(i){
					var elem=$(this);
					elem.attr("index",i);
					elem.bind("click",jqTab.clickTab);
					elem.bind("click",opt.func);
			});
		});
	},
	clickTab:function(e){
		e.stopPropagation();
		//e.preventDefault();	
		var curElem=$(this);
		var pe=curElem.parent();
		var oldTab=pe.find(".tab_sub_active");
		var oldIndex=oldTab.attr("index");
		oldTab.removeClass("tab_sub_active");
		curElem.addClass("tab_sub_active");
		var index=curElem.attr("index");
		var content=pe.parent().find("div."+pe.attr("content"));
		$(content.get(oldIndex)).hide();
		$(content.get(index)).show();
		jqTab.curTab=curElem;
		jqTab.curContent=$(content.get(index));
	}	
}
var dragHandle={
	isStart:false,
	winId:'listTreeDragWin',
	startDrag:function(e){
		$.initEvent(e);
		//右键不处理
		if(e.keyCode==3){
			return;
		}
		dragHandle.curOpt=$(e.src).data("dragOpt");
		dragHandle.isStart=true;
		dragHandle.oldPos=dragHandle.getPosition(e);
		dragHandle.init(e);
		if(dragHandle.curOpt.beforeDrag&&!dragHandle.curOpt.beforeDrag()){
			//添加拖动前置条件判断
			dragHandle.destroy();
			dragHandle.isStart=false;
			return;
		}
		dragHandle.win.show();
	},
	stopDrag:function(e){
		if(!this.isStart)
			return;
		this.isStart=false;
		this.curOpt.onDrop.call(this.srcElem,e);
		this.win.hide();
		this.destroy();
	},
	destroy:function(){
		this.srcElem=null;
		this.oldPos=null;
		this.curPos=null;
		this.offset=null;
		this.curOpt=null;
	},
	drag:function(e){
		if(!this.isStart)
			return;
		$.stopEvent(e);
		this.curPos=this.getPosition(e);
		var xLen=this.curPos.x-this.oldPos.x;
		var yLen=this.curPos.y-this.oldPos.y;
		this.win.css("top",this.offset.top+yLen);
		this.win.css("left",this.offset.left+xLen);
		if(this.curOpt.onDrag){
			this.curOpt.onDrag(e);
		}
	},
	getPosition:function(e){
		var pos={};
		pos.x=e.pageX?e.pageX:e.x;
		pos.y=e.pageY?e.pageY:e.y;
		return pos;
	},
	init:function(e){
		$.initEvent(e);
		this.srcElem=$(e.src);
		this.win=$("#"+this.winId);
		if(!this.win.length){
			this.win=$("<div id="+this.winId+" />").appendTo(document.body);
			this.win.hide();	
		}
		this.win.text($(e.src).text());
		var offset=$(e.src).offset();
		if(this.curOpt.isFocus){
			offset.left+=this.win.width()/2+20
		}
		this.offset=offset;
		this.win.css("top",offset.top);	
		this.win.css("left",offset.left);	
		this.win.width(this.srcElem.width());
	},
	checkInTarget:function(drag,obj){
		var d=this.win.offset();
		var o=obj.offset();
		var max={
			x:o.left+obj.width(),
			y:o.top+obj.height()	
		}
		if(d.top>=o.top&&d.top<=max.y&&d.left>=o.left&&d.left<=max.x)
			return true;
		else
			return false;
	},
	checkIsOver:function(obj){
		//取重心进行判断
		var d=this.win.offset();
		var pos={
			y:d.top+this.win.height()/2,
			x:d.left+this.win.width()/2
		}
		var o=obj.offset();
		//进入 1/3 触发事件
		var temp={
			min_x:o.left+obj.width()/5,
			min_y:o.top+obj.height()/5,
			max_x:o.left+obj.width(),
			max_y:o.top+obj.height()
		}
		if(pos.y>=temp.min_y&&pos.y<=temp.max_y&&pos.x>=temp.min_x&&pos.x<=temp.max_x)
			return true;
		else
			return false;
	},
	dragOver:function(arrs){
		var len=arrs.length;
		var item;
		for(var i=0;i<len;i++){
			item=$(arrs.get(i));
			if(this.checkIsOver(item)){
				return item;
			}
		}
		return null;
	}
}
dragHandle.myDrag=function(opt){
	opt.src.data("dragOpt",opt);
	opt.src.bind("mousedown",dragHandle.startDrag);
}
document.onselectstart=function(){
	if(dragHandle.isStart){
		return false;	
	}
}
$(document).bind("mousemove",function(e){
	dragHandle.drag(e);
});
$(document).bind("mouseup",function(e){
	dragHandle.stopDrag(e);
});	

var _treeHandle={
	tree:function(opt){
		if(!opt.elem.length)
			return;
		
		var temp=opt.elem.children(".absolute");
		temp.listTree(opt.param);
		var inst=temp.data("listTree");
		$.getJSON(opt.url, function(data) {		
			inst.body.empty();
			inst.curTop=inst.body;
			inst.bindTree(data.tree);
			inst=null;
		})
		opt.elem.children(".targetSpan").bind("click",function(e){
			$.stopEvent(e);
			var src=$(this);
			var offset=src.offset();
			temp.css({
				top:offset.top+src.height(),
				left:offset.left,
				zIndex:1000,
				width:src.width()
			}); 
			temp.show();
		})
		$(document).bind("click",function(){
			temp.hide();
		})
	}	
}
